package com.hongxun.budget.ui.fragment.sg;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.hongxun.budget.R;
import com.hongxun.budget.ui.activity.sg.SelectSuppliesActivity;
import com.hongxun.budget.ui.dialog.InputDialog3;
import com.hongxun.budget.ui.dialog.ListDialog;
import com.hongxun.budget.ui.dialog.NoticeDialog;
import com.hongxun.budget.ui.fragment.BaseListFragment;
import com.hongxun.budget.utils.RecyclerViewUtils;
import com.hongxun.budget.utils.SgDBUtils;
import com.hongxun.greenentity.sg.CGoodsDetailEntity;
import com.hongxun.interfaces.ClickListener;
import com.hongxun.interfaces.ResponseListener;
import com.jessewu.library.SuperAdapter;
import com.jessewu.library.view.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 物资清单
 */
public class ListInventory extends BaseListFragment {
    private String uuid;
    private SuperAdapter<CGoodsDetailEntity> superAdapter;
    private List<CGoodsDetailEntity> datas = new ArrayList<>();
    private List<String> labels = new ArrayList<>();

    public ListInventory() {
    }

    public List<CGoodsDetailEntity> getDatas() {
        return datas;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return RecyclerViewUtils.getLinearLayoutManagerForVertical(context);
    }

    @Override
    public void initView() {
        super.initView();
        superAdapter.setData(datas);
        for (String s : getResources().getStringArray(R.array.supplies)) {
            labels.add(s);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        Observable.just(uuid).subscribeOn(Schedulers.io()).map(new Func1<String, Boolean>() {
            @Override
            public Boolean call(String uuid) {
                datas.clear();
                datas.addAll(SgDBUtils.getCGdListAll(uuid));
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public SuperAdapter getAdapter() {
        uuid = getArguments().getString("uuid");
        superAdapter = new SuperAdapter<CGoodsDetailEntity>(R.layout.sg_content_mraa_adapter) {
            @Override
            public void bindView(ViewHolder viewHolder, final CGoodsDetailEntity bean, int i) {
                try {
                    ((TextView) viewHolder.getView(R.id.tv1)).setText(bean.getGDName());
                    ((TextView) viewHolder.getView(R.id.tv2)).setText(bean.getGDNum() + "");
                    ((TextView) viewHolder.getView(R.id.tv3)).setText(bean.getGDUnit());
                    viewHolder.getView(R.id.tv4).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            longClickDel(bean);
                        }
                    });
                } catch (NullPointerException e) {
                }
            }
        };
        superAdapter.setOnItemClickListener(new SuperAdapter.OnItemClickListener<CGoodsDetailEntity>() {
            @Override
            public void onClick(int i, CGoodsDetailEntity bean) {
                editCount(bean);
            }
        });
        superAdapter.setEmptyDataView(R.layout.view_empty);
        return superAdapter;
    }

    //输入数量
    private void editCount(final CGoodsDetailEntity bean) {
        Bundle bundle = new Bundle();
        bundle.putSerializable("bean", bean);
        InputDialog3 dialog3 = new InputDialog3();
        dialog3.setArguments(bundle);
        dialog3.setListener(new ResponseListener() {
            @Override
            public void response(Object... data) {
                updateBean((CGoodsDetailEntity) data[0], datas.indexOf(bean));
            }
        }).show(getFragmentManager(), "");
    }

    private void updateBean(CGoodsDetailEntity entity, final int dex) {
        Observable.just(entity).observeOn(Schedulers.io()).map(new Func1<CGoodsDetailEntity, Boolean>() {
            @Override
            public Boolean call(CGoodsDetailEntity detailEntity) {
                datas.set(dex, detailEntity);
                SgDBUtils.upDataCGoodsDetailEntity(0, detailEntity);
                return true;
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                superAdapter.notifyDataSetChanged();
            }
        });
    }

    private void longClickDel(final CGoodsDetailEntity wzqd) {
        new NoticeDialog()
                .setTitle("删除物资")
                .setMsg(wzqd.getGDName())
                .setCancelButton("取消", null)
                .setConfirmButton("删除", new ClickListener() {
                    @Override
                    public void click() {
                        datas.remove(wzqd);
                        superAdapter.notifyDataSetChanged();
                        SgDBUtils.delCGoodsDetailEntity(wzqd.getGuid());
                    }
                }).show(getFragmentManager(), "");
    }

    //添加物资
    public void addWz() {
        new ListDialog<String>()
                .setTitle("选择物资")
                .setDatas(labels)
                .setListener(new ResponseListener() {
                    @Override
                    public void response(Object... data) {
                        startActivity(new Intent(getActivity(), SelectSuppliesActivity.class)
                                .putExtra("uuid", uuid)
                                .putExtra("pos", labels.indexOf(data[0])));
                    }
                }).show(getFragmentManager(), "k");

    }
}
